package com.travel.api;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.travel.dto.PriceDTO;
import com.travel.pojo.ScenicSpot;
import com.travel.pojo.UserOrder;
import com.travel.result.Result;
import com.travel.service.ScenicSpotService;
import com.travel.service.UserOrderService;
import com.travel.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

/**
 * @program: travel
 * @author: lvyaxin
 * @create: 2024-07-07 20:10
 **/
@RestController
@RequestMapping("/api/agent")
@Api(tags = "业务员管理接口")
public class AgentController {

    @Autowired
    private UserOrderService userOrderService;

    @Autowired
    private ScenicSpotService scenicSpotService;

    // 07-07 根据业务员id查询全部订单
    @GetMapping("/list/{id}")
    @ApiOperation("根据业务员id查询全部订单")
    public Result<List<UserOrder>> listById(@PathVariable String id){
        LambdaQueryWrapper<UserOrder> wrapper = new LambdaQueryWrapper<UserOrder>()
                .eq(UserOrder::getAgentId, id)
                .eq(UserOrder::getDeleteStatus, 0)
                .orderByDesc(UserOrder::getAddTime);
        List<UserOrder> list = userOrderService.list(wrapper);

        return Result.ok(list);
    }

    // 07-07 根据业务员id和状态查询订单
    @GetMapping("/list/{id}/{state}")
    @ApiOperation("根据业务员id和订单状态查询订单")
    public Result<List<UserOrder>> listByState(@PathVariable String id, @PathVariable String state){
        LambdaQueryWrapper<UserOrder> wrapper = new LambdaQueryWrapper<UserOrder>()
                .eq(UserOrder::getAgentId, id)
                .eq(UserOrder::getState, state)
                .eq(UserOrder::getDeleteStatus, 0)
                .orderByDesc(UserOrder::getAddTime);;
        List<UserOrder> list = userOrderService.list(wrapper);
        return Result.ok(list);
    }

    // 业务员修改价格
    @PutMapping("/updatePrice")
    @ApiOperation("业务员修改价格")
    public Result updatePrice(@RequestBody PriceDTO priceDTO){
        ScenicSpot scenicSpot = scenicSpotService.getById(priceDTO.getId());
        scenicSpot.setAdultFee(Double.valueOf(priceDTO.getAdultFee()));
        scenicSpot.setOldStuFee(Double.valueOf(priceDTO.getOldStuFee()));
        scenicSpot.setChildrenFee(Double.valueOf(priceDTO.getChildrenFee()));
        scenicSpot.setModifyTime(DateUtils.getNowTime());
        scenicSpotService.updateById(scenicSpot);
        return Result.ok();
    }

    // TODO 生成二维码
    @ApiOperation("生成二维码")
    @GetMapping("/generateQrCode")
    public Result generateQrCode(HttpServletRequest request, HttpServletResponse response) {
        String agentId = request.getParameter("agentId");
        BufferedImage qrCodeImage = QrCodeUtil
                .generate("https://huaqing.xgxtravel.com/startApp?agentId=" + agentId, 300, 300);
        response.setContentType("image/png");
        try {
            ImageIO.write(qrCodeImage, "png", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Result.ok();
    }
}
